import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    goodList: [
      {
        name: '诸葛亮',
        price: 1000,
        num: 1,
        checked: false
      },
      {
        name: '蔡文姬',
        price: 1500,
        num: 1,
        checked: false
      },
      {
        name: '妲己',
        price: 2000,
        num: 1,
        checked: false
      },
      {
        name: '鲁班',
        price: 2200,
        num: 1,
        checked: true
      }
    ]
  },
  getters: {
    // 计算总价
    getsumall (state) {
      return state.goodList.filter(item => item.checked === true)
        .reduce((pav, item) => pav + item.num * item.price, 0)
    }
  },
  mutations: {
    btnDel (state, index) {
      state.goodList.splice(index, 1)
    },
    // 数量加减
    btnchange (state, obj) {
      state.goodList[obj.index].num = obj.num
    },
    changcke (state, payload) {
      state.goodList[payload].checked = !state.goodList[payload].checked
    },
    checkedchange (state, val) {
      console.log(val)
      state.goodList.forEach(item => {
        // eslint-disable-next-line no-return-assign
        return item.checked = val
      })
    }

  },
  actions: {
  },
  modules: {
  }
})
